package controllers

import (
	"strings"
	"web-beego.com/v1/models"
)

type HomeController struct {
	BaseController
}

func (c *HomeController) Index() {
	c.checkLogin()
	c.setTpl()
}

func (c *HomeController) Login() {
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["headcssjs"] = "home/login_headcssjs.html"
	c.LayoutSections["footerjs"] = "home/login_footerjs.html"
	c.setTpl("home/login.html", "shared/layout_base.html")
}

func (c *HomeController) DoLogin() {
	username := strings.TrimSpace(c.GetString("UserName"))
	pwd := strings.TrimSpace(c.GetString("UserPwd"))
	if len(username) == 0 || len(pwd) == 0 {
		c.streamToJsonResult(models.FAIL, "用户名或密码不能为空", "")
	}
	user, err := models.UserQueryByName(username, pwd)
	if user != nil && err == nil {
		c.SetUserToSession(*user)
		c.streamToJsonResult(models.SUCCESS, "登录成功", "")
	} else {
		c.streamToJsonResult(models.FAIL, "用户名或密码验证失败", "")
	}
}
